package com.cmsky.open.food.controller.admin;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.web.scope.Flash;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.cmsky.open.base.manager.AreaInfoManager;
import com.cmsky.open.base.manager.GoodsClassifyInfoManager;
import com.cmsky.open.base.model.AreaInfo;
import com.cmsky.open.base.vo.query.GoodsClassifyInfoQuery;
import com.cmsky.open.core.base.BasePager;
import com.cmsky.open.core.base.BaseSpringController;
import com.cmsky.open.core.util.JsonUtils;
import com.cmsky.open.core.util.PagerUitl;
import com.cmsky.open.food.manager.FoodAreaInfoManager;
import com.cmsky.open.food.manager.FoodInfoManager;
import com.cmsky.open.food.model.FoodInfo;
import com.cmsky.open.food.vo.query.FoodInfoQuery;
import com.cmsky.open.shop.manager.ShopAreaInfoManager;
import com.cmsky.open.shop.manager.ShopInfoManager;
import com.cmsky.open.shop.vo.query.ShopAreaInfoQuery;

/**
 * @author jandyfish email:jandyfish(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


@Controller("admin.food.foodInfo")
@RequestMapping("/admin/food/foodInfo")
public class FoodInfoController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	protected static final Integer GOODS_FOOD_ROOT_ID = 7; //以后改成系统配置
	
	private FoodInfoManager foodInfoManager;
	private AreaInfoManager areaInfoManager;
	private FoodAreaInfoManager foodAreaInfoManager;
	private ShopInfoManager shopInfoManager;
	private ShopAreaInfoManager shopAreaInfoManager;
	private GoodsClassifyInfoManager goodsClassifyInfoManager;
	
	private final String LIST_ACTION = "redirect:/admin/food/foodInfo/list.do";
	
	public FoodInfoController() {
	}
	
	/** 
	 * 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写
	 **/
	public void setFoodInfoManager(FoodInfoManager manager) {
		this.foodInfoManager = manager;
	}

	public void setAreaInfoManager(AreaInfoManager areaInfoManager) {
		this.areaInfoManager = areaInfoManager;
	}

	public void setShopInfoManager(ShopInfoManager shopInfoManager) {
		this.shopInfoManager = shopInfoManager;
	}

	public void setShopAreaInfoManager(ShopAreaInfoManager shopAreaInfoManager) {
		this.shopAreaInfoManager = shopAreaInfoManager;
	}

	public void setFoodAreaInfoManager(FoodAreaInfoManager foodAreaInfoManager) {
		this.foodAreaInfoManager = foodAreaInfoManager;
	}

	public void setGoodsClassifyInfoManager(
			GoodsClassifyInfoManager goodsClassifyInfoManager) {
		this.goodsClassifyInfoManager = goodsClassifyInfoManager;
	}

	/**
	 * 增加了@ModelAttribute的方法可以在本controller的方法调用前执行,可以存放一些共享变量,如枚举值
	 */
	@ModelAttribute
	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
		GoodsClassifyInfoQuery query = new GoodsClassifyInfoQuery();
		query.setPid(GOODS_FOOD_ROOT_ID);
		model.put("foodTypes", JsonUtils.jsonFromObject(this.goodsClassifyInfoManager.findPage(query)));
	}
	
	/** 
	 * 执行搜索 
	 **/
	@RequestMapping("list")
	public ModelAndView list(HttpServletRequest request,HttpServletResponse response,FoodInfoQuery query) {
		BasePager page = this.foodInfoManager.findPage(query);
		
		ModelAndView result = new ModelAndView("/admin/food/foodInfo/list");
		result.addAllObjects(toModelMap(page, query));
		return result;
	}
	
	/**
	 * 默认管理页
	 */
	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("/admin/food/foodInfo/index");
	}
	/**
	 * ajax搜索
	 * @param request
	 * @param response
	 * @param query
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/ajaxList")
	public String ajaxList(HttpServletRequest request,HttpServletResponse response,FoodInfoQuery query) {
		BasePager<FoodInfo> page = null;
		if(null==query.getAreaId() || 0 >= query.getAreaId()){
			page = this.foodInfoManager.findPage(query);
			if(null!=page.getRows()){
				for(FoodInfo f : page.getRows()){
					f.setAreaInfos(this.foodAreaInfoManager.findAreaInfosByFoodid(f.getId()));
					f.setShopInfo(this.shopInfoManager.getById(f.getShopId()));
				}
			}
		}else{
			page = this.foodInfoManager.findByKey(query);
			int count = this.foodInfoManager.countByKey(query);
			if(null!=page.getRows()){
				for(FoodInfo f : page.getRows()){
					f.setAreaInfos(this.foodAreaInfoManager.findAreaInfosByFoodid(f.getId()));
					f.setShopInfo(this.shopInfoManager.getById(f.getShopId()));
				}
			}
		}
		return JsonUtils.jsonFromObject(PagerUitl.pasreToLigerMap(page));
	}
	/**
	 * 批量更新或新增
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveOrUpdate")
	public String saveOrUpdate(HttpServletRequest request,HttpServletResponse response){
		String json = request.getParameter("json");
		Map<String,Object> result = new HashMap<String,Object>();
		List<Integer> successId = new LinkedList<Integer>();
		List tempList = JsonUtils.listFromJson(json);
		for(Object obj : tempList){
			FoodInfo bean = JsonUtils.objectFromJson(JsonUtils.jsonFromObject(obj),FoodInfo.class);
			if(this.foodInfoManager.saveOrUpdate(bean) > 0){
				successId.add(bean.getId());
			}
		}
		result.put("sucessList", successId);
		return JsonUtils.jsonFromObject(result);
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping("show")
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		FoodInfo foodInfo = (FoodInfo)foodInfoManager.getById(id);
		return new ModelAndView("/admin/food/foodInfo/show","foodInfo",foodInfo);
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping("create")
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,FoodInfo foodInfo) throws Exception {
		return new ModelAndView("/admin/food/foodInfo/create","foodInfo",foodInfo);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping("save")
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,FoodInfo foodInfo) throws Exception {
		foodInfoManager.save(foodInfo);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		FoodInfo foodInfo = (FoodInfo)foodInfoManager.getById(id);
		return new ModelAndView("/admin/food/foodInfo/edit","foodInfo",foodInfo);
	}
	
	/**
	 * 获取foodInfo对应的商户所有授权的社区
	 **/
	@RequestMapping("getAreas")
	@ResponseBody
	public String editAreas(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		FoodInfo foodInfo = (FoodInfo)foodInfoManager.getById(id);
		List<AreaInfo> usedAreaInfo = foodAreaInfoManager.findAreaInfosByFoodid(id);
		ShopAreaInfoQuery shopAreaInfoQuery = new ShopAreaInfoQuery();
		shopAreaInfoQuery.setShopId(foodInfo.getShopId());
		shopAreaInfoQuery.setGoodsClassifyId(foodInfo.getType());
		List<AreaInfo> allAreaInfo = this.shopAreaInfoManager.findAreasByShopId(shopAreaInfoQuery);
		Map<String,Object> view = new HashMap<String,Object>();
		view.put("foodInfo",foodInfo);
		view.put("usedAreaInfo", usedAreaInfo);
		view.put("allAreaInfo", allAreaInfo);
		return JsonUtils.jsonFromObject(view);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping("update")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		java.lang.Integer id = new java.lang.Integer(request.getParameter("id"));
		
		FoodInfo foodInfo = (FoodInfo)foodInfoManager.getById(id);
		bind(request,foodInfo);
		foodInfoManager.update(foodInfo);
		Flash.current().success(UPDATE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping("delete")
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] items = request.getParameterValues("items");
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			
			java.lang.Integer id = new java.lang.Integer((String)params.get("id"));
			
			foodInfoManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return new ModelAndView(LIST_ACTION);
	}
	
}

